function [bad_events,bad_channels]=osl_detect_badevent(S)


Dx = spm_eeg_epochs(S);


chan_list=find(strcmp(chantype(Dx),S.modality));
dat=Dx(chan_list,:,:);

S2=[];
S2.D=Dx;
S2.chanind=chan_list;
S2.threshold=S.artifact_amplitude*std(dat(:));
matx = spm_eeg_artefact_threshchan(S2);
a=sum(matx,2);
b=S.false_rate*length(events(Dx));
bad_chan=find(a>=b);
matx(bad_chan,:)=0;
bad_events=find(sum(matx,1)>1);
bad_channels=chan_list(bad_chan);


% eog_list=find(strcmp(chantype(Dx),'EOG'));
% dat=Dx(eog_list,:,:);
% S=[];
% S.D=Dx;
% S.chanind=eog_list;
% S.threshold=5*std(dat(:));
% matx = spm_eeg_artefact_threshchan(S);
% a=sum(matx,2);
% b=mean(a(a>0))+std(a(a>0));
% bad_channel=find(a>=b);
% matx(bad_channel,:)=0;
% bad_events=[bad_events find(sum(matx,1)>0)];


bad_events=unique(bad_events); 

delete(Dx);